//
//  BookCommentViewController.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 12-12-10.
//  Copyright (c) 2012年 wzyk. All rights reserved.
//

#import "ListenCommentViewController.h"
#import "ListenCommentCell.h"

@interface ListenCommentViewController ()

@end

@implementation ListenCommentViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    commentTableView.hidden = YES;
    nothingView.hidden = YES;
    [self getData];
}

- (void)getData
{
    [self showLoadingView];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSDictionary *data = [[DataRequest sharedRequest] getListenComment:_listenID];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self removeloadingView];
            if (data && [data isKindOfClass:[NSDictionary class]] && [[data objectForKey:@"result"] isKindOfClass:[NSArray class]]) {
                self.commentArray = [data objectForKey:@"result"];
                [self updateComment];
            }
        });
    });
}

- (void)updateComment
{
    [commentTableView reloadData];
    [commentTableView setContentOffset:CGPointMake(0, 0) animated:YES];
    if (_commentArray.count == 0) {
        commentTableView.hidden = YES;
        nothingView.hidden = NO;
    }
    else {
        commentTableView.hidden = NO;
        nothingView.hidden = YES;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.commentArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ListenCommentCell *cell = [commentTableView dequeueReusableCellWithIdentifier:@"ListenCommentCell"];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ListenCommentCell" owner:self options:nil] objectAtIndex:0];
    }
    cell.commentInfo = [self.commentArray objectAtIndex:indexPath.row];
    [cell updateDisplay];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *commentInfo = [self.commentArray objectAtIndex:indexPath.row];
    NSString *comment = [commentInfo valueForKey:@"content"];
    CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(300, 99999) lineBreakMode:NSLineBreakByWordWrapping];
    return 60 - 21 + size.height;
}

- (void)showLoadingView
{
    if (loadingView.superview) {
        return;
    }
    if (loadingView == nil) {
        loadingView = [[[ABSpringLoadingView alloc] initWithFrame:self.view.bounds] autorelease];
    }
    loadingView.alpha = 0;
    [self.view addSubview:loadingView];
    [UIView animateWithDuration:0.3 animations:^{
        loadingView.alpha = 1;
    }];
}

- (void)removeloadingView
{
    if (loadingView) {
        [UIView animateWithDuration:0.3 animations:^{
            loadingView.alpha = 0;
        } completion:^(BOOL finished) {
            [loadingView removeFromSuperview];
            loadingView = nil;
        }];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [commentTableView release];
    [_listenID release];
    [_commentArray release];
    [nothingView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [commentTableView release];
    commentTableView = nil;
    [nothingView release];
    nothingView = nil;
    [super viewDidUnload];
}
@end
